package com.twang.stock.strategy;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.dto.StockMinutesMessage;
import com.twang.stock.entity.CompanyCodeInfo;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.rocketMq.RocketMQProducer;
import com.twang.stock.service.jinyan.CommonService;
import com.twang.stock.task.old.BaseTask;
import com.twang.stock.threadPool.AsycTaskExecutorConfig;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 1分钟成交额1000万，涨幅大于2
 */
@Slf4j
@Component
public class Stock60DayMaxVol extends BaseTask {


    @Autowired
    private AsycTaskExecutorConfig asycTaskExecutorConfig;

    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;


    @Autowired
    private RedisUtil redisUtil;

    private static String COMPANY_STOCK_CODE_LIST = "COMPANY_STOCK_CODE_LIST_";

    @Autowired
    private RocketMQProducer rocketMQProducer;


    @Autowired
    private CommonService commonService;


    @Scheduled(cron = "0 20 15 * * ?")
    public void excute() throws InterruptedException {
        if (!isWorkTradeTime() || "CLOSE".equals(commonService.getValue("WARN_MAX_VOL_ID_STATUS"))) {
            return;
        }
        initMinutesData();
    }


    public void initMinutesData() throws InterruptedException {
        try {
            List<CompanyCodeInfo> list;
            Object object = redisUtil.get(COMPANY_STOCK_CODE_LIST + HolidayCopy.getRealToday());
            if (null != object) {
                list = (List<CompanyCodeInfo>) object;
            } else {
                list = companyCodeInfoMapper.selectAll();
                redisUtil.set(COMPANY_STOCK_CODE_LIST + HolidayCopy.getRealToday(), list);
            }
            List<CompanyCodeInfo> list1 = list.stream().filter(item -> !(item.getShortName().contains("退") || item.getShortName().contains("B") || item.getShortName().contains("*ST") || item.getShortName().contains("ST") || item.getShortName().contains("*"))).collect(Collectors.toList());
            StringBuffer code = new StringBuffer("");
            int i = 0;
            for (CompanyCodeInfo companyCodeInfo : list1) {
                if (i < 100) {
                    code.append(companyCodeInfo.getCompanyCode()).append(",");
                } else {
                    i=0;
                    String finalCode = code.toString();
                    asycTaskExecutorConfig.taskQueueExecutor().execute(() -> {
                        try {
                            covertDTO(finalCode);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    });
                    code = new StringBuffer("");
                    code.append(companyCodeInfo.getCompanyCode()).append(",");
                }
                i++;
            }
            covertDTO(code.toString());
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     *
     * @param code
     * @throws InterruptedException
     */
    private void covertDTO(String code) throws InterruptedException {
        StockMinutesMessage message = new StockMinutesMessage();
        message.setMinutes(HolidayCopy.getHHmmss().substring(8, 12));
        message.setDate(HolidayCopy.getRealToday());
        message.setType(StockMinutesMessage.TYPE_60_DAY_MAX_VOL_UPDATE);
        message.setCode(code);
        log.info(">>>>>>>>>>>> covertDTO  message={}",JSONObject.toJSONString(message));
        rocketMQProducer.sendOneWay(JSONObject.toJSONString(message));
    }
}
